import http from '../utils/http';

/**
 * 获取所有维修记录
 * @returns {Promise<AjaxResult>} 返回所有维修记录
 */
export const getAllRecords = async () => {
  try {
    const response = await http.get('/records');
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 分页获取维修记录列表
 * @param {number} pageNum - 当前页码
 * @param {number} pageSize - 每页数量
 * @returns {Promise<AjaxResult>} 返回分页维修记录数据
 */
export const getRecordsByPage = async (pageNum = 1, pageSize = 10) => {
  try {
    const response = await http.get(`/records/page?pageNum=${pageNum}&pageSize=${pageSize}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 根据ID获取维修记录
 * @param {number} id - 维修记录ID
 * @returns {Promise<AjaxResult>} 返回维修记录详情
 */
export const getRecordById = async (id) => {
  try {
    const response = await http.get(`/records/${id}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 创建新的维修记录
 * @param {Object} recordData - 维修记录数据
 * @returns {Promise<AjaxResult>} 返回创建结果
 */
export const createRecord = async (recordData) => {
  try {
    const response = await http.post('/records', recordData);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 更新维修记录
 * @param {number} id - 维修记录ID
 * @param {Object} recordData - 更新的维修记录数据
 * @returns {Promise<AjaxResult>} 返回更新结果
 */
export const updateRecord = async (id, recordData) => {
  try {
    const response = await http.put(`/records/${id}`, recordData);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 删除维修记录
 * @param {number} id - 维修记录ID
 * @returns {Promise<AjaxResult>} 返回删除结果
 */
export const deleteRecord = async (id) => {
  try {
    const response = await http.delete(`/records/${id}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 根据用户ID获取维修记录
 * @param {number} userId - 用户ID
 * @returns {Promise<AjaxResult>} 返回该用户的维修记录
 */
export const getRecordsByUserId = async (userId) => {
  try {
    const response = await http.get(`/records/user/${userId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 根据会议室ID获取维修记录
 * @param {number} roomId - 会议室ID
 * @returns {Promise<AjaxResult>} 返回该会议室的维修记录
 */
export const getRecordsByRoomId = async (roomId) => {
  try {
    const response = await http.get(`/records/room/${roomId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

/**
 * 条件分页查询维修记录
 * @param {Object} params - 查询参数
 * @param {number} params.pageNum - 当前页码
 * @param {number} params.pageSize - 每页数量
 * @param {Object} params.condition - 查询条件
 * @returns {Promise<AjaxResult>} 返回分页维修记录数据
 */
export const getRecordsByCondition = async (params) => {
  const { pageNum = 1, pageSize = 10, condition = {} } = params;
  try {
    const response = await http.post(`/records/page/condition?pageNum=${pageNum}&pageSize=${pageSize}`, condition);
    return response.data;
  } catch (error) {
    console.error('条件查询出错:', error);
    throw error;
  }
};

/**
 * 客户报修接口
 * @param {Object} reportData - 报修数据
 * @param {number} reportData.userId - 用户ID
 * @param {number} reportData.roomId - 会议室ID
 * @param {string} reportData.problemDesc - 问题描述
 * @returns {Promise<AjaxResult>} 返回报修结果
 */
export const reportMaintenance = async (reportData) => {
  try {
    const response = await http.post('/records/report', reportData);
    return response.data;
  } catch (error) {
    console.error('提交报修失败:', error);
    throw error;
  }
};
